package com.mayikt;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: maplefire
 * @Description:
 * @Date: 2022/09/15 20:24
 */

public class Test02 implements Runnable {

    // set和get必须要是同一个lock锁
    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        set();
    }


    private void set() {
        try {
            lock.lock();
            System.out.println("set方法");
            get();
        } finally {
            lock.unlock();
        }
    }

    private void get() {
        try {
            lock.lock();
            System.out.println("lock 可以具备可重入性-get方法");
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Test02 test02 = new Test02();
        new Thread(test02).start();
        new Thread(test02).start();
    }
}
